package com.maybach.web.controller.certificate;

import com.maybach.common.po.PersonCertificatePO;
import com.maybach.core.ao.CertificateAO;
import com.maybach.core.ao.FlowApplyAO;
import com.maybach.core.ao.UserAO;
import com.maybach.core.form.flow.TaskActForm;
import com.maybach.core.form.user.UserQueryForm;
import com.maybach.util.query.support.PageResult;
import com.maybach.web.constants.WebConstants;
import com.maybach.web.framework.util.WebUtil;
import com.maybach.workflow.common.api.enums.FlowStateEnum;
import com.maybach.workflow.common.model.FlowApplyDO;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;

/**
 * Created by houenxun on 16/1/22.
 * 用户信息认证
 */
@Controller
@RequestMapping("/certificate/person/")
public class PersonCertificateController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UserAO userAO;

    @Autowired
    private FlowApplyAO flowApplyAO;

    @Autowired
    private CertificateAO certificateAO;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }


    /**
     * 用户认证管理页面
     *
     * @return
     */
    @RequiresAuthentication
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(@ModelAttribute("userQueryForm") UserQueryForm userQueryForm, BindingResult bindingResult, ModelMap model) {
        if (null == userQueryForm) {
            userQueryForm = new UserQueryForm();
        }
        if (!model.containsAttribute("userListQuery")) {
            model.addAttribute(userQueryForm);
        }
        try {
            if (null == userQueryForm.getStatus()) {
                userQueryForm.setStatus(Arrays.asList(FlowStateEnum.init.getStatus(), FlowStateEnum.reject.getStatus()));
            }
            PageResult<FlowApplyDO> result = certificateAO.queryPersonCertificate(userQueryForm);
            model.addAttribute("pageResult", result);
            return "certificate/person/list";

        } catch (Exception e) {
            logger.error("", e);
            return "result";
        }
    }

    @RequiresAuthentication
    @RequestMapping(value = "audit")
    public String audit(@RequestParam("taskId") Long taskId, @RequestParam(required = false, value = "redirectUrl") String redirectUrl, Model model) {

        model.addAttribute(WebConstants.REDIRECT_URL_KEY, redirectUrl);

        FlowApplyDO applyDO = flowApplyAO.getApplyByTaskId(taskId);
        model.addAttribute("applyDO", applyDO);
        model.addAttribute("applyContent", applyDO.getContentObject());

        if (!model.containsAttribute("taskActForm")) {
            TaskActForm taskActForm = new TaskActForm();
            taskActForm.setTaskId(taskId);
            model.addAttribute("taskActForm", taskActForm);
        }
        return "certificate/person/audit";
    }

    /**
     * 通用审核操作
     *
     * @param request
     * @param redirectUrl
     * @param taskActForm
     * @param bindingResult
     * @param model
     * @param redirectAttributes
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "doAudit", method = RequestMethod.POST)
    @RequiresAuthentication
    public String doAudit(HttpServletRequest request, @RequestParam(required = false, value = "redirectUrl") String redirectUrl, @Valid @ModelAttribute("taskActForm") TaskActForm taskActForm, BindingResult bindingResult, ModelMap model, RedirectAttributes redirectAttributes) throws Exception {
        if (bindingResult.hasErrors()) {
            WebUtil.storeFlashAttribute(redirectAttributes, model);
            return "redirect:" + WebUtil.getLastUrlFromReferer(request);
        }

        model.addAttribute(WebConstants.REDIRECT_URL_KEY, redirectUrl);

        FlowApplyDO flowApplyDO = flowApplyAO.getApplyByTaskId(taskActForm.getTaskId());
        PersonCertificatePO certificatePO = (PersonCertificatePO) flowApplyDO.getContentObject();
        certificatePO.setMemo(taskActForm.getMemo());
        flowApplyDO.setContentObject(certificatePO);
        flowApplyAO.updateApply(flowApplyDO);
        flowApplyAO.executeTask(taskActForm);
        return WebConstants.COMMON_RESULT_PAGE;
    }


}
